<?php
/**
 * Created by PhpStorm.
 * User: 69548
 * Date: 2018/8/22
 * Time: 15:02
 */

namespace app\wap\controller;

class Token extends Basewap
{

    public function index(){
        $member_id=  input('member_id');
        if (empty($member_id)) return  json_encode(['msg'=>'参数错误','code'=>101]);
        $t= db('member')->where('member_id',$member_id)->find();
        if (empty($t['token'])) {
            if (!empty($t['member_username'])){
                $token = md5(md5($t['member_username']) + md5($t['wechat_unionid']) + md5(time()) . '_wap');
                $b = db('member')->where('member_id', $t['member_id'])->update([
                    'token' => $token,
                    'token_time' => time()
                ]);
                if ($b == false) return json_encode(['msg' => '数据写入失败', 'code' => 101]);
            }
        }

        $d=db('coupondetails')
            ->alias('a')
            ->where('a.member_id', $t['member_id'])
            ->join('coupon d','a.coupon_id=d.coupon_id')
            ->where('a.usestatus',0)
            ->field('d.*,a.id,a.starttime')
            ->select();
        $jishu=0;
        foreach ($d as $v){
            if ($v['end_usetime'] < time()) {
            } else {
                $jishu++;
            }
        }
        $kbxcoupon=  new Kbxcoupon();
        $dai= $kbxcoupon->usercoupon($t['member_id']);
        if (!empty($dai)){
            $dai= json_decode($dai ,true);
            foreach ($dai as $j){
                if($j['locked']!=true && $j['amount']!=0){
                    $jishu++;
                }
            }
        }

        $img='';
        if (empty($t['thumb'])){
            $img='';
        }else{
            $img=  db('attachment')->where('id',$t['thumb'])->find();
            $img=$img['filepath'];
        }

        $Integral= db('integral')->select();

        if($t['member_pcode']==1159){
            $partner_id = 'fl';
        }else{
            $partner_id = 'kbx';
        }

        $shu=[
            'member_id'=>$t['member_id'],
            'is_admin'=>$t['is_admin'],
            'member_username'=>$t['member_username'],
            'member_nickname'=>$t['member_nickname'],
            'member_integral'=>$t['member_integral'],
            'member_money'=>$t['member_money'],
            'is_apply'=>$t['is_apply'],
            'member_coupon'=>$jishu,
            'img'=>$img,
            'token'=>!empty($token)?$token:$t['token'],
            'Integral'=>$Integral,
            'partner_id'=>$partner_id,
            'member_kbx_id'=>$t['kbx_id'],
        ];

        $this->longInvite($member_id,5);//常驻邀新
//        $this->getNewYear($member_id,4);//新年活动

        return json_encode(['msg'=>'更新成功','data'=>$shu,'code'=>0]);
    }



    //常驻邀新活动任务
    public function longInvite($member_id,$mk_id=5,$order_id='longInvite'){
        $data=db('coupon_marketing')->where(['mk_id'=>$mk_id])->find();
        //先判断活动状态
        if($data['status'] == 2 ){
            $arr=['code'=>1,'msg'=>'活动已结束','data'=>''];
            return $arr;
        }
        if($data['time_chose'] == 2 && $data['endtime'] < time()){
            $arr=['code'=>1,'msg'=>'活动已结束','data'=>''];
            return $arr;
        }
        if($data['time_chose'] == 2 && $data['starttime'] > time()){
            $arr=['code'=>1,'msg'=>'活动尚未开始','data'=>''];
            return $arr;
        }
        //获取优惠券
        if(empty($data['coupon_ids'])){
            $arr=['code'=>1,'msg'=>'旅行红包没有啦','data'=>''];
            return $arr;
        }else{
            $count_list_market=explode(',',$data['coupon_ids']);
        }

        $where['member_id'] = $member_id;
        $where['order_id'] = $order_id;
        $temp_share=db('share_register')->where($where)->select();
        if(empty($temp_share)){
            //添加
            if(count($count_list_market) > 1){
                $list_temp_all=[];
                foreach ($count_list_market as $k){
                    $list_temp=[
                        'member_id'=>$member_id,
                        'createtime'=>time(),
                        'order_id'=>$order_id,
                        'status'=>1,
                        'sharenum'=>$data['share'],
                        'coupon_id'=>$k, // 追加优惠券id
                        'goods_id'=>'',
                        'type'=>4
                    ];
                    array_push($list_temp_all,$list_temp);
                }
                db('share_register')->insertAll($list_temp_all);
            }else{
                $list_temp=[
                    'member_id'=>$member_id,
                    'createtime'=>time(),
                    'order_id'=>$order_id,
                    'status'=>1,
                    'sharenum'=>$data['share'],
                    'coupon_id'=>$count_list_market[0],// 追加优惠券id
                    'goods_id'=>'',
                    'type'=>4
                ];
                db('share_register')->insert($list_temp);
            }
        }
        return true;
    }


    //新年活动  发放红包
    public function getNewYear($member_id,$mk_id=4,$order_id='新年活动',$goods_id=''){
        $data=db('coupon_marketing')->where(['mk_id'=>$mk_id])->find();
        //先判断活动状态
        if($data['status'] == 2 ){
            $arr=['code'=>1,'msg'=>'活动已结束','data'=>''];
            return $arr;
        }
        if($data['time_chose'] == 2 && $data['endtime'] < time()){
            $arr=['code'=>1,'msg'=>'活动已结束','data'=>''];
            return $arr;
        }

//        暂时写死！！！
//        if($data['time_chose'] == 2 && $data['starttime'] > time()){
//            $arr=['code'=>1,'msg'=>'活动尚未开始','data'=>''];
//            return $arr;
//        }
        //获取优惠券
        if(empty($data['coupon_ids'])){
            $arr=['code'=>1,'msg'=>'旅行红包没有啦','data'=>''];
            return $arr;
        }else{
            $count_list_market=explode(',',$data['coupon_ids']);
        }

        $temp_share=db('share_register')->where(['member_id'=>$member_id,'order_id'=>$order_id ])->select();
        if(empty($temp_share)){
            //添加
            if(count($count_list_market) > 1){
                $list_temp_all=[];
                foreach ($count_list_market as $k){
                    $list_temp=[
                        'member_id'=>$member_id,
                        'createtime'=>time(),
                        'order_id'=>$order_id,
                        'status'=>2,
                        'sharenum'=>$data['share'],
                        'coupon_id'=>$k, // 追加优惠券id
                        'goods_id'=>$goods_id,
                        'type'=>3
                    ];
                    array_push($list_temp_all,$list_temp);
                }
                db('share_register')->insertAll($list_temp_all);
            }else{
                $list_temp=[
                    'member_id'=>$member_id,
                    'createtime'=>time(),
                    'order_id'=>$order_id,
                    'status'=>2,
                    'sharenum'=>$data['share'],
                    'coupon_id'=>$count_list_market[0],// 追加优惠券id
                    'goods_id'=>$goods_id,
                    'type'=>3
                ];
                db('share_register')->insert($list_temp);
            }
        }
        return true;
    }


}